local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local LeveClass=require(script.Parent.Parent.Parent.Level)

-- 刷怪控制器
function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.LevelIdList=self.parent.LevelIdList
    self:InitLevels()
end;

-- 初始化关卡
function M.InitLevels(self)
    self.LevelCount=#self.LevelIdList --章节配置关卡的数量
    self.LevelIndex=0 --当前关卡索引
    self.LevelList={}
    for k,id in pairs(self.LevelIdList) do --初始化所有关卡
        local level= LeveClass.new(id,self.parent) 
        self.LevelList[level:GetId()]=level
        -- print("-----InitLevels--------->",id)
        level.PassEvent:Connect(function(lvId) --开始下一关
            -- print("----通关了-------------",lvId)
            self:AddProcess(function()
                self:LevelPassDataSave(lvId)
                task.wait()
                self:NextLevel()
            end)

        end)
     end
end 

--获取当前关卡
function M.GetActiveLevel(self)
    local lvId=self.LevelIdList[self.LevelIndex]
    -- print("--------GetActiveLevel-------------",lvId,self.LevelIndex)
    local level=  self.LevelList[lvId]
    assert(level,"Level==nil",self.LevelIndex.."/"..lvId)
    return level
end

-- 刷一关怪
function M.NextLevel(self)
    local nextLevelIndex=self.LevelIndex+1
    if nextLevelIndex>self.LevelCount  then
        self.parent:Victory(true)
    else
        self.LevelIndex=nextLevelIndex
        local nowLevel=self:GetActiveLevel()
        nowLevel:Action()
       self.parent:SetWaveProgress(self.LevelIndex,self.LevelCount,nowLevel:MonsterCount())
    end
end
local InstanceManager = CommonOHOH.load.InstanceManager();

function M.GetAllMonster(self)

   local list={}
    local attctrl=self.parent:GetPrivateComponent("AttCtrl")
   for _,v in pairs(attctrl:FindMonsterFolder():GetChildren()) do
        table.insert(list, InstanceManager:FindObject(v));
   end

    return list
end

function M.GetSortMonsterByDistance(self,character) 
    local npos=character:GetPosition()
    local SortList={}
    for _,m in pairs(self:GetAllMonster()) do
        if m and m:IsCanAttack(character) then
            local mPos=m:GetPosition()
           local d=(mPos -npos).Magnitude
           table.insert(SortList,{
             obj=m, --攻击目标
             dis=d,
             pos=mPos,--攻击位置
           })
        end
    end

    -- 按离玩家距离排序
    table.sort(SortList,function(a,b)
        return a.dis<b.dis
    end)

    return SortList
end

-- 寻找最近的怪物
function M.FindNearMonster(self,character,...) 

    local list=self:GetSortMonsterByDistance(character)
    if #list>0 then
        return list[1]
    end
end


function M.dtor(self)

    for k,level in pairs(self.LevelList) do
        level:dtor()
    end

    self.LevelList={}

    for _, m in pairs(self:GetAllMonster()) do
        m:dtor()
    end

	M.super.dtor(self);
end;

-- 开始刷怪
function M.Action(self)
    self:NextLevel()
   
end

--通关数据处理
function M.LevelPassDataSave(self,levelId)
    for _,player in pairs(self.parent:GetAllPlayerList()) do
        pcall(function()
            player.LevelPassEvent:Action(levelId)
        end)
    end
end


return M